#!/usr/bin/env python
#-*- coding: utf-8 -*-
#
# Copyright 2013 Antoine Drouin (poinix@gmail.com)
#
# This file is part of PAT.
#
#    PAT is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    PAT is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with PAT.  If not, see <http://www.gnu.org/licenses/>.
#

import math
import numpy as np
import scipy
import scipy.integrate
import scipy.signal
import matplotlib.pyplot as plt

import pat.vehicles.rotorcraft.dynamic_model_synth as dm
import pat.utils as pu


my_dm = dm.DynamicModel()
print "Param:\n", my_dm.P

Xe, Ue = my_dm.trim()
print "Ue ", Ue
time = np.arange(0., 10., 0.01)

def run_sim(du, axis, title):
    U = Ue*np.ones((len(time),dm.iv_size))
    U[:, axis] += du
    X = np.zeros((len(time), dm.sv_size))               
    X[0] = my_dm.reset(Xe)
    for i in range(1,len(time)):
        X[i,:] = my_dm.run(time[i]-time[i-1], U[i-1])
    my_dm.plot_trajectory(time, X, U, window_title=title, filename="/tmp/{:s}.png".format(title))


du = 0.1*scipy.signal.square(time+math.pi/2.)
run_sim(du, dm.iv_lat, "step lat")

run_sim(du, dm.iv_lon, "step lon")

run_sim(du, dm.iv_rud, "step rud")

du = 0.5*scipy.signal.square(time+math.pi/2.)

run_sim(du, dm.iv_col, "step col")

run_sim(du, dm.iv_thr, "step thr")

plt.show()
